﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace FileRWDAO
{
    public class FileRWDAO
    {
        //引入DLL包 //函数申明
        #region
        //声明写入字段值
        [DllImport("kernel32")]//返回0表示失败，返回非0则表示成功
        private static extern long WritePrivateProfileString(String section, String key, String val, String filePath);
        //声明获取配置文件信息
        [DllImport("kernel32")]//返回取得字符串缓冲区的长度，该方法已经在外部dll文件中已经实现，无需再次在此实现，只需导入申明即可
        private static extern long GetPrivateProfileString(String section, String key, String def, StringBuilder retVal, int size, String filePath);
        #endregion

        //读取INI文件
        #region
        public static String readIniData(String section, String key, String fileName, String filePath)
        {
            try
            {
                String iniFilePath = "";
                //如果当前路径以\结尾则不增加\
                if (filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    filePath = filePath.Substring(0, filePath.Length - 1);
                }
                if (fileName.StartsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    fileName = fileName.Substring(1);
                }
                iniFilePath = filePath + Path.DirectorySeparatorChar.ToString() + fileName;
              //  File.AppendAllText("log_err.log", "FileRWDAO正在读取配置文件" + iniFilePath + "！\r\n");
                if (File.Exists(iniFilePath))
                {
                    String noText = "";
                    StringBuilder temp = new StringBuilder(1024);//创建文件读取缓冲区大小
                    GetPrivateProfileString(section, key, noText, temp, 1024, iniFilePath);
                    return temp.ToString();
                }
                else
                {
                    File.Create(iniFilePath);
                    return null;
                }
            }
            catch (Exception e1)
            {
                return null;
            }
        }
        public static bool fileExists(String fileName, String filePath)
        {
            String iniFilePath = "";
            //如果当前路径以\结尾则不增加\
            if (filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                iniFilePath = filePath + fileName;
            }
            else
            {
                iniFilePath = filePath + Path.DirectorySeparatorChar.ToString() + fileName;
            }
            if (File.Exists(iniFilePath))
                return true;
            else return false;
        }
        #endregion

        //写入文件
        #region
        public static bool WriteIniData(String section, String key, String value, String fileName, String filePath)
        {
            String iniFilePath = "";
            //如果当前路径以\结尾则不增加\
            if (filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                filePath = filePath.Substring(0, filePath.Length - 1);
            }
            if (fileName.StartsWith(Path.DirectorySeparatorChar.ToString()))
            {
                fileName = fileName.Substring(1);
            }
            iniFilePath = filePath + Path.DirectorySeparatorChar.ToString() + fileName;
            if (!File.Exists(iniFilePath))
            {
                FileStream fileStream = new FileStream(iniFilePath, FileMode.CreateNew);
                StreamWriter streamWriter = new StreamWriter(fileStream);
                streamWriter.Close();
                fileStream.Close();
            }
            long opStation = WritePrivateProfileString(section, key, value, iniFilePath);
            if (opStation == 0)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        #endregion
        //删除ini文件节点
        //读取INI文件
        #region
        public static String readIniData(String section, String key, String filePath)
        {
            try
            {
                filePath = filePath.Replace("\\\\","\\");
              //  File.AppendAllText("log_err.log", "FileRWDAO 111正在读取配置文件" + filePath + "！\r\n");
                String iniFilePath = "";
                //如果当前路径以\结尾则不增加\
                if (filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    iniFilePath = filePath;
                }
                else
                {
                    iniFilePath = filePath;
                }
                if (File.Exists(iniFilePath))
                {
                    String noText = "";
                    StringBuilder temp = new StringBuilder(1024);//创建文件读取缓冲区大小
                    GetPrivateProfileString(section, key, noText, temp, 1024, iniFilePath);
                    return temp.ToString();
                }
                else
                {
                    File.Create(iniFilePath);
                    return null;
                }
            }
            catch (Exception e1)
            {
                return null;
            }
        }
        public static String readDataFromIni(String section, String key, String fileName)
        {
            try
            {
                String iniFilePath = "";
                if (fileName.IndexOf(":\\") < 0)
                {
                    fileName = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.DirectorySeparatorChar + fileName;
                }
                //如果当前路径以\结尾则不增加\
                if (fileName.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    iniFilePath = fileName;
                }
                else
                {
                    iniFilePath = fileName;
                }
               // File.AppendAllText("log_err.log", "正在读取配置文件" + fileName + "！");
                if (File.Exists(iniFilePath))
                {
                    String noText = "";
                    StringBuilder temp = new StringBuilder(1024);//创建文件读取缓冲区大小
                    GetPrivateProfileString(section, key, noText, temp, 1024, iniFilePath);
                    return temp.ToString();
                }
                else return null;
            }
            catch (Exception e1)
            {
                return null;
            }
        }
        public static bool fileExists(String filePath)
        {
            String iniFilePath = "";
            //如果当前路径以\结尾则不增加\
            if (filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                iniFilePath = filePath;
            }
            else
            {
                iniFilePath = filePath;
            }
            if (File.Exists(iniFilePath))
                return true;
            else return false;
        }
        #endregion

        //写入文件
        #region
        public static bool WriteIniData(String section, String key, String value, String filePath)
        {
            String iniFilePath = "";
            //如果当前路径以\结尾则不增加\ 
            if (filePath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                iniFilePath = filePath;
            }
            else
            {
                iniFilePath = filePath;
            }
            if (!File.Exists(iniFilePath))
            {
                FileStream fileStream = new FileStream(iniFilePath, FileMode.CreateNew);
                StreamWriter streamWriter = new StreamWriter(fileStream);
                streamWriter.Close();
                fileStream.Close();
            }
            long opStation = WritePrivateProfileString(section, key, value, iniFilePath);
            if (opStation == 0)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        #endregion
        //写入文件
        #region
        public static bool WriteDataToIni(String section, String key, String value, String fileName)
        {
            String iniFilePath = "";
            if (fileName.IndexOf(":\\") < 0)
            {
                fileName = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.DirectorySeparatorChar + fileName;
            }
            //如果当前路径以\结尾则不增加\ 
            if (fileName.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                iniFilePath = fileName;
            }
            else
            {
                iniFilePath = fileName;
            }
            if (!File.Exists(iniFilePath))
            {
                FileStream fileStream = new FileStream(iniFilePath, FileMode.CreateNew);
                StreamWriter streamWriter = new StreamWriter(fileStream);
                streamWriter.Close();
                fileStream.Close();
            }
            long opStation = WritePrivateProfileString(section, key, value, iniFilePath);
            if (opStation == 0)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        #endregion
        //删除ini文件节点
    }
}
